﻿using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text;

namespace Helpers
{
    public class DiHelper
    {
        public static IServiceCollection Services { get; set; } = new ServiceCollection();

        private static object IServiceProviderLocker = new object();
        private static IServiceProvider _serviceProvider;
        public static IServiceProvider ServiceProvider
        {
            get
            {
                if (_serviceProvider == null)
                    lock (IServiceProviderLocker)
                    {
                        if (_serviceProvider == null)
                            _serviceProvider = CreateServiceProvider();
                    }
                return _serviceProvider;
            }
        }

        public static IServiceProvider CreateServiceProvider()
        {
            return Services?.BuildServiceProvider(); ;
        }

        public static IServiceProvider ReflashServiceProvider()
        {
            _serviceProvider = null;
            return ServiceProvider;
        }

    }
}
